package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public Result addAddress(@RequestBody AddressBook addressBook){
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    /**
     * 查询所有地址
     * @return
     */
    @GetMapping("/list")
    public Result selectAllAdderss(){
        //前端不需要传值，我们在业务层使用当前用户id来查询所有的地址铺
        List<AddressBook> addressBookList = addressBookService.selectAllAdderss();
        return Result.success(addressBookList);
    }
    /**
     * 根据id数据回显
     * @return
     */
    @GetMapping("/{id}")
    public Result selectByIdAddress(@PathVariable Integer id){
        AddressBook addressBook = addressBookService.selectByIdAddress(id);
        return Result.success(addressBook);
    }
    /**
     * 修该地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result updateByIdAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateByIdAddress(addressBook);
        return Result.success();
    }


    @DeleteMapping
    public Result deleteAddress(Integer id){
        addressBookService.deleteAddress(id);
        return Result.success();
    }


    @GetMapping("/default")
    public Result selectDefault(){
        AddressBook addressBook = addressBookService.selectDefault();
        return Result.success(addressBook);
    }


    @PutMapping("/default")
    public Result updateDefault(@RequestBody AddressBook addressBook){
        addressBookService.updateDefault(addressBook);
        return Result.success();
    }
}
